//
//  UIFont+NWWeight.m
//  mesu
//
//  Created by dl on 2018/7/4.
//  Copyright © 2018年 gdmesu. All rights reserved.
//

#import "UIFont+NWWeight.h"

NWFontWeight const NWFontWeightUltraLight = @"ultra_light";
NWFontWeight const NWFontWeightThin = @"thin";
NWFontWeight const NWFontWeightLight = @"light";
NWFontWeight const NWFontWeightRegular = @"regular";
NWFontWeight const NWFontWeightMedium = @"medium";
NWFontWeight const NWFontWeightSemibold = @"semibold";
NWFontWeight const NWFontWeightBold = @"bold";
NWFontWeight const NWFontWeightHeavy = @"heavy";
NWFontWeight const NWFontWeightBlack = @"black";

@interface UIFont (HBWeightPrivate)

+ (CGFloat)nw_fontWeightFromString:(NWFontWeight)weight __API_AVAILABLE(ios(8.2));
+ (BOOL)nw_fontWeightStringIsBold:(NWFontWeight)weight;

@end

@implementation UIFont (HBWeight)

+ (UIFont *)nw_systemFontOfSize:(CGFloat)fontSize weight:(NWFontWeight)weight
{
    /**
     * ultra_light
     * thin
     * light
     * regular
     * medium
     * semibold
     * bold
     * heavy
     * black
     */
    
    if (@available(iOS 8.2, *)) {
        CGFloat fontWeight = [self nw_fontWeightFromString:weight];
        return [UIFont systemFontOfSize:fontSize weight:fontWeight];
    }
    
    if ([self nw_fontWeightStringIsBold:weight]) { // below iOS 8.2
        return [UIFont boldSystemFontOfSize:fontSize];
    }
    
    return [UIFont systemFontOfSize:fontSize];
}

+ (UIFont *)nw_fontWithName:(NSString *)fontName size:(CGFloat)fontSize weight:(NWFontWeight)weight
{
    UIFont *font = nil;
    if (fontName) {
        font = [UIFont fontWithName:fontName size:fontSize];
    }
    
    if (!font) {
        font = [UIFont nw_systemFontOfSize:fontSize weight:weight];
    }
    
    return font;
}

@end

@implementation UIFont (HBWeightPrivate)

+ (CGFloat)nw_fontWeightFromString:(NWFontWeight)weight // for iOS 8.2+ only, will crash on lower versions
{
    NSString *weightString = [weight lowercaseString];
    
    if ([weightString isEqualToString:NWFontWeightUltraLight]) {
        return UIFontWeightUltraLight;
    }
    
    if ([weightString isEqualToString:NWFontWeightThin]) {
        return UIFontWeightThin;
    }
    
    if ([weightString isEqualToString:NWFontWeightLight]) {
        return UIFontWeightLight;
    }
    
    if ([weightString isEqualToString:NWFontWeightRegular]) {
        return UIFontWeightRegular;
    }
    
    if ([weightString isEqualToString:NWFontWeightMedium]) {
        return UIFontWeightMedium;
    }
    
    if ([weightString isEqualToString:NWFontWeightSemibold]) {
        return UIFontWeightSemibold;
    }
    
    if ([weightString isEqualToString:NWFontWeightBold]) {
        return UIFontWeightBold;
    }
    
    if ([weightString isEqualToString:NWFontWeightHeavy]) {
        return UIFontWeightHeavy;
    }
    
    if ([weightString isEqualToString:NWFontWeightBlack]) {
        return UIFontWeightBlack;
    }
    
    return UIFontWeightRegular;
}

+ (BOOL)nw_fontWeightStringIsBold:(NWFontWeight)weight // below iOS 8.2
{
    NSString *weightString = [weight lowercaseString];
    
    if ([weightString isEqualToString:NWFontWeightBold] || [weightString isEqualToString:NWFontWeightHeavy] || [weightString isEqualToString:NWFontWeightBlack]) {
        return YES;
    }
    
    return NO;
}

@end
